home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Log / win.php < prev   
PHP Script  |  2004-10-01  |  8KB  |  257 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/win.php,v 1.16 2004/09/08 23:35:53 jon Exp $
  4.  *
  5.  * @version $Revision: 1.16 $
  6.  * @package Log
  7.  */
  8.  
  9. /**
  10.  * The Log_win class is a concrete implementation of the Log abstract
  11.  * class that logs messages to a separate browser window.
  12.  *
  13.  * The concept for this log handler is based on part by Craig Davis' article
  14.  * entitled "JavaScript Power PHP Debugging:
  15.  *
  16.  *  http://www.zend.com/zend/tut/tutorial-DebugLib.php
  17.  * 
  18.  * @author  Jon Parise <jon@php.net>
  19.  * @since   Log 1.7.0
  20.  * @package Log
  21.  *
  22.  * @example win.php     Using the window handler.
  23.  */
  24. class Log_win extends Log
  25. {
  26.     /**
  27.      * The name of the output window.
  28.      * @var string
  29.      * @access private
  30.      */
  31.     var $_name = 'LogWindow';
  32.  
  33.     /**
  34.      * The title of the output window.
  35.      * @var string
  36.      * @access private
  37.      */
  38.     var $_title = 'Log Output Window';
  39.  
  40.     /**
  41.      * Mapping of log priorities to colors.
  42.      * @var array
  43.      * @access private
  44.      */
  45.     var $_colors = array(
  46.                         PEAR_LOG_EMERG   => 'red',
  47.                         PEAR_LOG_ALERT   => 'orange',
  48.                         PEAR_LOG_CRIT    => 'yellow',
  49.                         PEAR_LOG_ERR     => 'green',
  50.                         PEAR_LOG_WARNING => 'blue',
  51.                         PEAR_LOG_NOTICE  => 'indigo',
  52.                         PEAR_LOG_INFO    => 'violet',
  53.                         PEAR_LOG_DEBUG   => 'black'
  54.                     );
  55.  
  56.     /**
  57.      * String buffer that holds line that are pending output.
  58.      * @var array
  59.      * @access private
  60.      */
  61.     var $_buffer = array();
  62.  
  63.     /**
  64.      * Constructs a new Log_win object.
  65.      * 
  66.      * @param string $name     Ignored.
  67.      * @param string $ident    The identity string.
  68.      * @param array  $conf     The configuration array.
  69.      * @param int    $level    Log messages up to and including this level.
  70.      * @access public
  71.      */
  72.     function Log_win($name, $ident = '', $conf = array(),
  73.                           $level = PEAR_LOG_DEBUG)
  74.     {
  75.         $this->_id = md5(microtime());
  76.         $this->_name = $name;
  77.         $this->_ident = $ident;
  78.         $this->_mask = Log::UPTO($level);
  79.  
  80.         if (isset($conf['title'])) {
  81.             $this->_title = $conf['title'];
  82.         }
  83.         if (isset($conf['colors']) && is_array($conf['colors'])) {
  84.             $this->_colors = $conf['colors'];
  85.         }
  86.  
  87.         register_shutdown_function(array(&$this, '_Log_win'));
  88.     }
  89.  
  90.     /**
  91.      * Destructor
  92.      */
  93.     function _Log_win()
  94.     {
  95.         if ($this->_opened || (count($this->_buffer) > 0)) {
  96.             $this->close();
  97.         }
  98.     }
  99.  
  100.     /**
  101.      * The first time open() is called, it will open a new browser window and
  102.      * prepare it for output.
  103.      *
  104.      * This is implicitly called by log(), if necessary.
  105.      *
  106.      * @access public
  107.      */
  108.     function open()
  109.     {
  110.         if (!$this->_opened) {
  111.             $win = $this->_name;
  112.  
  113.             if (!empty($this->_ident)) {
  114.                 $identHeader = "$win.document.writeln('<th>Ident</th>')";
  115.             } else {
  116.                 $identHeader = '';
  117.             }
  118.  
  119.             echo <<< END_OF_SCRIPT
  120. <script language="JavaScript">
  121. $win = window.open('', '{$this->_name}', 'toolbar=no,scrollbars,width=600,height=400');
  122. $win.document.writeln('<html>');
  123. $win.document.writeln('<head>');
  124. $win.document.writeln('<title>{$this->_title}</title>');
  125. $win.document.writeln('<style type="text/css">');
  126. $win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
  127. $win.document.writeln('td,th { font-size: 8pt; }');
  128. $win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
  129. $win.document.writeln('td,th { border-right: #999999 solid 1px; }');
  130. $win.document.writeln('</style>');
  131. $win.document.writeln('</head>');
  132. $win.document.writeln('<body>');
  133. $win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
  134. $win.document.writeln('<tr><th>Time</th>');
  135. $identHeader
  136. $win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
  137. </script>
  138. END_OF_SCRIPT;
  139.             $this->_opened = true;
  140.         }
  141.  
  142.         return $this->_opened;
  143.     }
  144.  
  145.     /**
  146.      * Closes the output stream if it is open.  If there are still pending
  147.      * lines in the output buffer, the output window will be opened so that
  148.      * the buffer can be drained.
  149.      *
  150.      * @access public
  151.      */
  152.     function close()
  153.     {
  154.         /*
  155.          * If there are still lines waiting to be written, open the output
  156.          * window so that we can drain the buffer.
  157.          */
  158.         if (!$this->_opened && (count($this->_buffer) > 0)) {
  159.             $this->open();
  160.         }
  161.  
  162.         if ($this->_opened) {
  163.             $this->_writeln('</table>');
  164.             $this->_writeln('</body></html>');
  165.             $this->_opened = false;
  166.         }
  167.  
  168.         return ($this->_opened === false);
  169.     }
  170.  
  171.     /**
  172.      * Writes a single line of text to the output window.
  173.      *
  174.      * @param string    $line   The line of text to write.
  175.      *
  176.      * @access private
  177.      */
  178.     function _writeln($line)
  179.     {
  180.         /* Add this line to our output buffer. */
  181.         $this->_buffer[] = $line;
  182.  
  183.         /* Buffer the output until this page's headers have been sent. */
  184.         if (!headers_sent()) {
  185.             return;
  186.         }
  187.  
  188.         /* If we haven't already opened the output window, do so now. */
  189.         if (!$this->_opened && !$this->open()) {
  190.             return false;
  191.         }
  192.  
  193.         /* Drain the buffer to the output window. */
  194.         $win = $this->_name;
  195.         foreach ($this->_buffer as $line) {
  196.             echo "<script language='JavaScript'>\n";
  197.             echo "$win.document.writeln('" . addslashes($line) . "');\n";
  198.             echo "self.focus();\n";
  199.             echo "</script>\n";
  200.         }
  201.  
  202.         /* Now that the buffer has been drained, clear it. */
  203.         $this->_buffer = array();
  204.     }
  205.  
  206.     /**
  207.      * Logs $message to the output window.  The message is also passed along
  208.      * to any Log_observer instances that are observing this Log.
  209.      * 
  210.      * @param mixed  $message  String or object containing the message to log.
  211.      * @param string $priority The priority of the message.  Valid
  212.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  213.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  214.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  215.      * @return boolean  True on success or false on failure.
  216.      * @access public
  217.      */
  218.     function log($message, $priority = null)
  219.     {
  220.         /* If a priority hasn't been specified, use the default value. */
  221.         if ($priority === null) {
  222.             $priority = $this->_priority;
  223.         }
  224.  
  225.         /* Abort early if the priority is above the maximum logging level. */
  226.         if (!$this->_isMasked($priority)) {
  227.             return false;
  228.         }
  229.  
  230.         /* Extract the string representation of the message. */
  231.         $message = $this->_extractMessage($message);
  232.  
  233.         list($usec, $sec) = explode(' ', microtime());
  234.  
  235.         /* Build the output line that contains the log entry row. */
  236.         $line  = '<tr align="left" valign="top">';
  237.         $line .= sprintf('<td>%s.%s</td>',
  238.                          strftime('%T', $sec), substr($usec, 2, 2));
  239.         if (!empty($this->_ident)) {
  240.             $line .= '<td>' . $this->_ident . '</td>';
  241.         }
  242.         $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
  243.         $line .= sprintf('<td style="color: %s">%s</td>',
  244.                          $this->_colors[$priority],
  245.                          preg_replace('/\r\n|\n|\r/', '<br />', $message));
  246.         $line .= '</tr>';
  247.  
  248.         $this->_writeln($line);
  249.  
  250.         $this->_announce(array('priority' => $priority, 'message' => $message));
  251.  
  252.         return true;
  253.     }
  254. }
  255.  
  256. ?>
  257.